#include<stdio.h>
#include<sstream>
#include<vector>
#include<string>
#include "xmlParser.h"
#include "dataControl.h"
#include <sys/types.h>
#include <db.h>
#include <string.h>

#define DATABASE "lotniska.db"


//using namespace std;
std::vector<lotnisko> pobierz_lotniska()
{
	DB *dbp;
	DBT key, data1;
	int ret, t_ret;
	char buff[5000];
	string lotniska;
	
	if ((ret = db_create(&dbp, NULL, 0)) != 0) 
	{ 
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
	}

	if ((ret = dbp->open( dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) 
	{
		dbp->err(dbp, ret, "%s", DATABASE); 
	}

	memset(&key, 0, sizeof(key));
	memset(&data1, 0, sizeof(data1));
	key.data = (void *) "lotniska";
	key.size = sizeof("lotniska");
	
	if ((ret = dbp->get(dbp, NULL, &key, &data1, 0)) == 0)
		;//printf("db: %s: key retrieved: data was %s.\n", (char *)key.data, (char *)data1.data);
err:if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
		ret = t_ret; 
	strncpy(buff, (char *)data1.data, 5000);
	buff[4999] = 0;
	XMLNode xMainNode=XMLNode::parseString(buff);
	int n = xMainNode.nChildNode();//nowe
	XMLNode xNode;
	lotnisko tmp;
	int iterator = 0;
	std:: vector <lotnisko> resultVector;
	
	while(iterator<n)
	{
		std::stringstream ss1, ss3, ss4, ss5, ss6, ss7;
		xNode = xMainNode.getChildNode(iterator);
		
		//konwersja typow
		ss1<<xNode.getAttribute("id_lotniska");
		ss1>>tmp.id_lotniska;
		tmp.nazwa.assign(xNode.getAttribute("nazwa"));
		ss3<<xNode.getAttribute("pojemnosc");
		ss3>>tmp.pojemnosc;
		ss4<<xNode.getAttribute("szerokosc_geo");
		ss4>>tmp.szerokosc_geo;
		ss5<<xNode.getAttribute("dlugosc_geo");
		ss5>>tmp.dlugosc_geo;
		ss6<<xNode.getAttribute("ilosc_szerokich_pasow");
		ss6>>tmp.ilosc_szerokich_pasow;
		ss7<<xNode.getAttribute("ilosc_waskich_pasow");
		ss7>>tmp.ilosc_waskich_pasow;
		if (xNode.getAttribute("aktywnosc")[0] == '+')
			tmp.aktywnosc = true;
		else
			tmp.aktywnosc = false;
			
		resultVector.push_back(tmp);
		iterator++;
	}
	return resultVector;
}


std::vector<lotnisko_dla_samolotu> pobierz_pozycje()
{
	DB *dbp;
	DBT key, data1;
	int ret, t_ret;
	char buff[5000];
	//string lotniska;
	
	if ((ret = db_create(&dbp, NULL, 0)) != 0) 
	{ 
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
	}

	if ((ret = dbp->open( dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) 
	{
		dbp->err(dbp, ret, "%s", DATABASE); 
	}

	memset(&key, 0, sizeof(key));
	memset(&data1, 0, sizeof(data1));
	key.data = (void *) "lotniska";
	key.size = sizeof("lotniska");
	
	if ((ret = dbp->get(dbp, NULL, &key, &data1, 0)) == 0)
		;//printf("db: %s: key retrieved: data was %s.\n", (char *)key.data, (char *)data1.data);
err:if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
		ret = t_ret; 
	//cout<<(char *)data1.data<<endl;
	//string lotniska((char *)data1.data);
	//cout<<lotniska.c_str()<<endl;
	strncpy(buff, (char *)data1.data, 5000);
	buff[4999] = 0;
//	cout<<buff << endl;
//	XMLNode xMainNode=XMLNode::parseString(lotniska.c_str());
	XMLNode xMainNode=XMLNode::parseString(buff);
	int n = xMainNode.nChildNode();
	XMLNode xNode;
	lotnisko_dla_samolotu tmp;
	int iterator = 0;
	std::vector <lotnisko_dla_samolotu> resultVector;
	while(iterator<n)
	{
		std::stringstream ss1, ss2, ss3, ss4;
		cout<<iterator<<endl;
//		cout<<xMainNode.createXMLString(false)<<endl;
		xNode = xMainNode.getChildNode(iterator);
		//cout<<xMainNode.createXMLString(false)<<endl;
		//cout<<xNode.getAttribute("aktywnosc")[0]<<endl;
		if (xNode.getAttribute("aktywnosc")[0] == '+')
		{
			//cout<<"DEBUG3"<<endl;	
			
			//konwersja typow
			tmp.nazwa.assign(xNode.getAttribute("nazwa"));
			ss1<<xNode.getAttribute("id_lotniska");
			ss1>>tmp.id_lotniska;
			ss2<<xNode.getAttribute("szerokosc_geo");
			ss2>>tmp.szerokosc_geo;
			ss3<<xNode.getAttribute("dlugosc_geo");
			ss3>>tmp.dlugosc_geo;
			
			//cout<<"DEBUG4"<<endl;	
			resultVector.push_back(tmp);
			//cout<<"DEBUG5"<<endl;	
		}
		iterator++;
	}
	return resultVector;
}

std::vector<samolot> pobierz_samoloty()
{
	XMLNode xMainNode=XMLNode::openFileHelper("samoloty.xml", "Samoloty");
	int n = xMainNode.nChildNode();
	XMLNode xNode;
	samolot tmp;
	int iterator = 0;
	std:: vector <samolot> resultVector;
	
	while(iterator<n)
	{
		std::stringstream ss2, ss3, ss4, ss5;
		xNode = xMainNode.getChildNode(iterator);
		
		//konwersja typow
		tmp.nazwa.assign(xNode.getAttribute("nazwa"));
		ss2<<xNode.getAttribute("bak");
		ss2>>tmp.bak;
		ss3<<xNode.getAttribute("spalanie");
		ss3>>tmp.spalanie;
		ss4<<xNode.getAttribute("predkosc");
		ss4>>tmp.predkosc;
		ss5<<xNode.getAttribute("czas_obslugi");
		ss5>>tmp.czas_obslugi;
		if (xNode.getAttribute("czy_szeroki")[0] == '+')
			tmp.czy_szeroki = true;
		else
			tmp.czy_szeroki = false;
			
		resultVector.push_back(tmp);
		iterator++;
	}
	return resultVector;
}

std::vector<zdarzenie> pobierz_zdarzenia()
{
	XMLNode xMainNode=XMLNode::openFileHelper("zdarzenia.xml", "Zdarzenia");
	int n = xMainNode.nChildNode();
	XMLNode xNode;
	zdarzenie tmp;
	int iterator = 0;
	std:: vector <zdarzenie> resultVector;
	
	while(iterator<n)
	{
		std::stringstream ss2, ss3;
		xNode = xMainNode.getChildNode(iterator);
		
		//konwersja typow
		tmp.nazwa.assign(xNode.getAttribute("nazwa"));
		ss2<<xNode.getAttribute("typ");
		ss2>>tmp.typ;
		ss3<<xNode.getAttribute("podtyp");
		ss3>>tmp.podtyp;

		resultVector.push_back(tmp);
		iterator++;
	}
	return resultVector;
}

lotnisko lotnisko_po_id(int zadane_id)
{
	DB *dbp;
	DBT key, data1;
	int ret, t_ret;
	string lotniska;
	char buff[5000];
	
	if ((ret = db_create(&dbp, NULL, 0)) != 0) 
	{ 
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
	}

	if ((ret = dbp->open( dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) 
	{
		dbp->err(dbp, ret, "%s", DATABASE); 
	}

	memset(&key, 0, sizeof(key));
	memset(&data1, 0, sizeof(data1));
	key.data = (void *) "lotniska";
	key.size = sizeof("lotniska");
	
	if ((ret = dbp->get(dbp, NULL, &key, &data1, 0)) == 0)
		//printf("db: %s: key retrieved: data was %s.\n", (char *)key.data, (char *)data1.data);
err:if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
		ret = t_ret; 
	//cout<<(char *)data1.data<<endl;
	strncpy(buff, (char *)data1.data, 5000);
	buff[4999] = 0;
	XMLNode xMainNode=XMLNode::parseString(buff);
	//cout<<"tu1"<<endl;
	XMLNode xNode;
	lotnisko tmp;
	//konwersja typow
	std::stringstream ss;
	std::string id;
	ss << zadane_id;
	id = ss.str();
	xNode = xMainNode.getChildNodeWithAttribute("lotnisko", "id_lotniska", id.c_str(), 0);
	std::stringstream ss1, ss3, ss4, ss5, ss6, ss7;
	//konwersja typow
	ss1<<xNode.getAttribute("id_lotniska");
	ss1>>tmp.id_lotniska;
	tmp.nazwa.assign(xNode.getAttribute("nazwa"));
	ss3<<xNode.getAttribute("pojemnosc");
	ss3>>tmp.pojemnosc;
	ss4<<xNode.getAttribute("szerokosc_geo");
	ss4>>tmp.szerokosc_geo;
	ss5<<xNode.getAttribute("dlugosc_geo");
	ss5>>tmp.dlugosc_geo;
	ss6<<xNode.getAttribute("ilosc_szerokich_pasow");
	ss6>>tmp.ilosc_szerokich_pasow;
	ss7<<xNode.getAttribute("ilosc_waskich_pasow");
	ss7>>tmp.ilosc_waskich_pasow;
	if (xNode.getAttribute("aktywnosc")[0] == '+')
		tmp.aktywnosc = true;
	else
		tmp.aktywnosc = false;
	return tmp;
}

samolot_dla_lotniska obsluga_po_nazwie(std::string zadana_nazwa)
{
	XMLNode xMainNode=XMLNode::openFileHelper("samoloty.xml", "Samoloty");
	XMLNode xNode;
	samolot_dla_lotniska tmp;

	xNode = xMainNode.getChildNodeWithAttribute("samolot", "nazwa", zadana_nazwa.c_str(), 0);
	
	std::stringstream ss;
	//konwersja:
	ss<<xNode.getAttribute("czas_obslugi");
	ss>>tmp.czas_obslugi;
	if (xNode.getAttribute("czy_szeroki")[0] == '+')
		tmp.czy_szeroki = true;
	else
		tmp.czy_szeroki = false;
	
	return tmp;
}

samolot_dla_samolotu techniczne_po_nazwie(std::string zadana_nazwa)
{
	XMLNode xMainNode=XMLNode::openFileHelper("samoloty.xml", "Samoloty");
	XMLNode xNode;
	samolot_dla_samolotu tmp;

	xNode = xMainNode.getChildNodeWithAttribute("samolot", "nazwa", zadana_nazwa.c_str(), 0);
	
	std::stringstream ss1, ss2, ss3;
	//konwersja:
	ss1<<xNode.getAttribute("bak");
	ss1>>tmp.bak;
	ss2<<xNode.getAttribute("spalanie");
	ss2>>tmp.spalanie;
	ss3<<xNode.getAttribute("predkosc");
	ss3>>tmp.predkosc;
	
	return tmp;
}

int aktywuj_lotnisko(int id_lotniska)
{
	DB *dbp;
	DBT key, data1;
	int ret, t_ret;
	string lotniska;
	
	if ((ret = db_create(&dbp, NULL, 0)) != 0) 
	{ 
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
	}

	if ((ret = dbp->open( dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) 
	{
		dbp->err(dbp, ret, "%s", DATABASE); 
	}

	memset(&key, 0, sizeof(key));
	memset(&data1, 0, sizeof(data1));
	key.data = (void *) "lotniska";
	key.size = sizeof("lotniska");
	
	if ((ret = dbp->get(dbp, NULL, &key, &data1, 0)) == 0)
		;//printf("db: %s: key retrieved: data was %s.\n", (char *)key.data, (char *)data1.data);
	//lotniska.assign((char *)data1.data);
	char buff[5000];
	strncpy(buff, (char *)data1.data, 5000);
	buff[4999] = 0;
	XMLNode xMainNode=XMLNode::parseString(buff);
	XMLNode xNode;
	std::stringstream ss;
	std::string tmp;

	ss<<id_lotniska;
	ss>>tmp;
	xNode = xMainNode.getChildNodeWithAttribute("lotnisko", "id_lotniska", tmp.c_str(), 0);
	xNode.updateAttribute("+", NULL, "aktywnosc");
	lotniska.assign(xMainNode.createXMLString(false));
	data1.data = (void *) lotniska.c_str();
	data1.size = lotniska.length();
	
	
	if ((ret = dbp->put(dbp, NULL, &key, &data1, 0)) == 0) 
		printf("db: %s: key stored.\n", (char *)key.data); 
	else 
	{ 
		dbp->err(dbp, ret, "DB->put"); 
		//goto err; 
	}
	
	err:if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
		ret = t_ret; 
	return 1;
}

int deaktywuj_lotnisko(int id_lotniska)
{
	DB *dbp;
	DBT key, data1;
	int ret, t_ret;
	string lotniska;
	
	if ((ret = db_create(&dbp, NULL, 0)) != 0) 
	{ 
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
	}

	if ((ret = dbp->open( dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) 
	{
		dbp->err(dbp, ret, "%s", DATABASE); 
	}

	memset(&key, 0, sizeof(key));
	memset(&data1, 0, sizeof(data1));
	key.data = (void *) "lotniska";
	key.size = sizeof("lotniska");
	
	if ((ret = dbp->get(dbp, NULL, &key, &data1, 0)) == 0)
		;//printf("db: %s: key retrieved: data was %s.\n", (char *)key.data, (char *)data1.data);

	char buff[5000];
	strncpy(buff, (char *)data1.data, 5000);
	buff[4999] = 0;
	XMLNode xMainNode=XMLNode::parseString(buff);
	XMLNode xNode;
	std::stringstream ss;
	std::string tmp;

	ss<<id_lotniska;
	ss>>tmp;
	xNode = xMainNode.getChildNodeWithAttribute("lotnisko", "id_lotniska", tmp.c_str(), 0);
	xNode.updateAttribute("-", NULL, "aktywnosc");
	lotniska.assign(xMainNode.createXMLString(false));
	data1.data = (void *) lotniska.c_str();
	data1.size = lotniska.length();
	
	
	if ((ret = dbp->put(dbp, NULL, &key, &data1, 0)) == 0) 
		printf("db: %s: key stored.\n", (char *)key.data); 
	else 
	{ 
		dbp->err(dbp, ret, "DB->put"); 
		//goto err; 
	}
	
	err:if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
		ret = t_ret; 
	return 1;
}
